﻿'use strict';

var app = app || {}; // establish application global namespace

app.SCOPES = 'https://www.googleapis.com/auth/userinfo.email'; +
            ' https://www.googleapis.com/auth/userinfo.profile';
app.CookieValue = 'LOGGED-IN';
app.CRM_CLIENT = 'isha';
//app.RESPONSE_TYPE = 'token id_token';

app.mode = 'DEVE'; // UAT, LIVE
switch (app.mode) {
    case 'DEVE': {
        // crmdnaweb
        app.CLIENT_ID = '539005674085.apps.googleusercontent.com';
        app.API_ROOT = 'https://crmdnaweb.appspot.com/_ah/api';
        app.API_KEY = 'AIzaSyDyiCwfHk7aLhOfjIKu1fvAbZT_-6BHNts'
        break;
    }
    case 'UAT': {
        // crmdnaserver
        app.CLIENT_ID = '220866004543.apps.googleusercontent.com';
        app.API_ROOT = 'https://crmdnaserver.appspot.com/_ah/api';
        app.API_KEY = 'AIzaSyA_NekYKdGFES1kJ3oh6ofLTvftn2ut-Sg';
        break;
    }
}

angular.module('ishaCrmApp', ['ngRoute', 'ngCookies'])
    .config(['$routeProvider', '$provide', function ($routeProvider, $provide) {

        // $onRootScope decorator on the $rootScope object
        $provide.decorator('$rootScope', ['$delegate', function ($delegate) {

            Object.defineProperty($delegate.constructor.prototype, '$onRootScope', {
                value: function (name, listener) {
                    var unsubscribe = $delegate.$on(name, listener);
                    this.$on('$destroy', unsubscribe);
                },
                enumerable: false
            });
            return $delegate;
        }]);

        // Routing
        $routeProvider.when('/search', { templateUrl: 'scripts/app/templates/search.html', controller: 'searchCtrl' });
        $routeProvider.when('/', { templateUrl: 'scripts/app/templates/home.html', controller: 'homeCtrl' });
        $routeProvider.when('/member/:id', { templateUrl: 'scripts/app/templates/member.html', controller: 'memberCtrl' });
        $routeProvider.otherwise({ redirectTo: '/' });

    }]);

function init() {

    var callback = function () {
        if (--apisToLoad == 0) {
            angular.bootstrap(document, ['ishaCrmApp']);
        }
    }

    var apisToLoad = 4; // must match number of calls to gapi.client.load()
    //gapi.client.setApiKey(app.API_KEY);
    gapi.client.load('user', 'v1', callback, app.API_ROOT);
    gapi.client.load('member', 'v1', callback, app.API_ROOT);
    gapi.client.load('group', 'v1', callback, app.API_ROOT);
    gapi.client.load('oauth2', 'v2', callback);
};